import time

from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains as AC

driver_path = r'C:\Program Files\Google\Chrome\Application\chromedriver.exe'
Chrome_Service = Service(executable_path=driver_path)
chrome_driver = webdriver.Chrome(service=Chrome_Service)

# 登录元素
input_username = By.ID, 'ls_username'
input_password = By.ID, 'ls_password'
login_button = By.XPATH, '//*[@id="lsform"]/div/div/table/tbody/tr[2]/td[3]/button/em'
loginsubmit = By.NAME, 'loginsubmit'
source_element = '/html/body/div/div[3]/div[2]/div[6]'
target_elenmet = '/html/body/div/div[3]/div[1]/div[1]/html/body/div/div[3]/div[1]/div[1]'

# 断言元素

# 用例数据
username = 'mysouls'
password = 'imFrede007'

chrome_driver.get('https://www.chiphell.com/forum-146-1.html')
chrome_driver.maximize_window()

wait_load = WebDriverWait(chrome_driver, 10, 0.5).until(
    EC.presence_of_element_located(input_username)
)
chrome_driver.find_element(*input_username).send_keys(username)

chrome_driver.find_element(*input_password).send_keys(password)

wait_login_success = WebDriverWait(chrome_driver, 10, 0.5).until(
    EC.presence_of_element_located(login_button)
)

chrome_driver.find_element(*login_button).click()
wait_login_submit = WebDriverWait(chrome_driver, 10, 0.5).until(
    EC.presence_of_element_located(loginsubmit)
)
chrome_driver.find_element(*loginsubmit).click()

actions = AC(chrome_driver)
actions.drag_and_drop(*source_element, *target_elenmet).perform()

print("登录")
time.sleep(15)
